﻿// Serotonin -- easy-to-use image filters application
// https://code.google.com/p/serotonin/
//
// Copyright © Frank Nagl, 2012-2013
// admin@franknagl.de
//

using System.Globalization;

namespace Serotonin.OptionsForms
{
    using Filter;
    using Serotonin.Controls;
    using System;
    using System.Drawing;
    using System.Windows.Forms;        

    /// <summary>Filter specified properties form.</summary>
    public partial class GaussianBlurForm : Form, IOptionsForm
    {
        private readonly GaussianBlur filter;

        /// <summary>
        /// The processor for managing the images and the image GUI panel.
        /// </summary>
        public Processor Processor { get; set; }

        /// <summary>
        /// Initializes a new instance of the 
        /// <see cref="GaussianBlurForm"/> class.
        /// </summary>
        /// <param name="processor">The processor.</param>
        public GaussianBlurForm(Processor processor)
        {
            InitializeComponent();
            filter = AOF.Init(processor, this, gbProcess,
                                new GaussianBlur()) as GaussianBlur;
        }

                /// <summary> Applies the specified filter in the options form. </summary>
        public void ApplyFilter()
        {
            // grayscale example
            Bitmap b = filter.Apply(Processor.OriginalImage);            
            string s = Program.L.Text[40] + Program.L.Text[105] + ": " +
                        "Sigma=" + filter.Sigma + "; " +
                        "Size=" + filter.Size + "; ";
            Processor.Change(b, s); 
        }

        /// <summary>
        /// Sets the language specified string texts for the form elements.
        /// </summary>
        public void SetStrings()
        {
            // Process groupbox
            gbProcess.Text = Program.L.Text[54];
            btnProcess.Text = Program.L.Text[55];
            btnReset.Text = Program.L.Text[56];

            Options.Text = Program.L.Text[105]; // Name of filter
            tabctrlOptions.TabPages[0].Text = Program.L.Text[57]; // Options

            gbDummy.Text = Program.L.Text[58];
            lbSigmaText.Text = Program.L.Text[106];
            lbSizeText.Text = Program.L.Text[106];
        }

        private void BtnProcessClick(object sender, EventArgs e)
        {
            EndlessProgressBarFormInThread form =
                new EndlessProgressBarFormInThread(
                    Program.L.Text[63],
                    Program.L.Text[64]);
            form.Start();

            try
            {
                ApplyFilter();
            }
            catch (ArgumentException aex)
            {
                MessageBox.Show(aex.Message);
                BtnResetClick(sender, e);
            }

            form.End();
        }

        private void BtnResetClick(object sender, EventArgs e)
        {
            Processor.Reset();
        }

        private void TrbarSigmaValueChanged(object sender, EventArgs e)
        {
            filter.Sigma = trbarSigma.Value / 1000.0f;
            lbSigma.Text = filter.Sigma.ToString(CultureInfo.InvariantCulture);
            Processor.ApplyFilterRTP();
        }

        private void TrbarSizeValueChanged(object sender, EventArgs e)
        {
            filter.Size = 2 * trbarSize.Value + 1;
            lbSize.Text = filter.Size.ToString(CultureInfo.InvariantCulture);
            Processor.ApplyFilterRTP();
        }
    }
}
